/**
 * 
 */
package com.d6game.test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author dell
 *
 */
public class CalculateDemo {

	public static void main(String[] args) {
		CountDownLatch startSignal = new CountDownLatch(1);  
        CountDownLatch doneSignal = new CountDownLatch(10); 
        ExecutorService service = Executors.newFixedThreadPool(10);  
        List<Future<Integer>> list = new ArrayList<Future<Integer>>();
        for (int i = 1; i <= 10; i++) {
        	list.add(service.submit(new CalculateCall(startSignal, doneSignal, i)));
		}
        startSignal.countDown();
        System.out.println("******************START***********************");
        try {
			doneSignal.await();
			System.out.println("****************AWAIT*****************");
		} catch (InterruptedException e) {
			
		}
        service.shutdown();
        int total =0;
        for (Future<Integer> f : list) {  
        	try {
        		Integer result = f.get();
        		total += result;
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (ExecutionException e) {
				e.printStackTrace();
			}  
        }
        System.out.println(total);
	}

}
